package com.threadmethod.wait;

/**
 * @PackageName:com.threadmethod.wait
 * @ClassName:Test08
 * @Description: notify()通知过早
 * @Author: zhangxiaoyu
 * @date 2021/1/11 17:14
 */
public class Test08 {

    public static void main(String[] args){
         final  Object lock = new Object();
         Thread t1= new Thread(new Runnable() {
             @Override
             public void run() {
                 synchronized (lock){
                     try {
                         System.out.println("bengin");
                         lock.wait();
                         System.out.println("end");
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
             }
         });

         Thread t2 = new Thread(new Runnable() {
             @Override
             public void run() {
                 synchronized (lock){
                     System.out.println("begin notify");
                     lock.notify();
                     System.out.println("end notify");
                 }
             }
         });

         //如果先开启t1,再开启t2，大多数情况下，t1先等待，t1再把t1唤醒
         t1.start();
         t2.start();

        //如果先开启2通知线程，再开启1线程，可能会出现1线程等待没收到通知的情况
        //t2.start();
        //t1.start();

        //实际上，调用start就是告诉线程调度器，当前线程准备就绪，线程调度器在什么时候开启这个线程不确定，即调用start()，开启线程的书讯，不一定
       // 就是线程实际开启的顺序
    }
}
